/*
Document   : logic
Created on : Oct 28, 2009, 2:58:03 PM
Author     : fdcube
 */

//Define ajax instances
var iins,sins,gins,uins,oins;
var SessionId;
var OlUid = 0;
var MsgId = 0;
var MsgColor = "black";
var LocalUserName;
//Define logic functions and callbacks
function Login(UserName,UserPwd) {
    LocalUserName = UserName;
    SendData = "{\"MsgType\":\"i\",\"UserName\":\"" + UserName + "\",\"UserPwd\":\"" + UserPwd + "\"}";
    doSend("i",SendData);
}
function LoginCallBack() {
    if (iins.readyState == 4){
        if (iins.status && iins.status == 200) {
            if (iins.responseText == "0") {
                alert("Wrong Password OR Username !\n Please Try Again");
            } else if (iins.responseText == "2") {
                alert("Server Error ! \nPlease Send The Details To fdcube@gmail.com\nWe Will Be Very Happy To Recieve Your Letter! ");
                window.location.reload();
            } else {
                LoginHa(iins.responseText);
                GetMsg();
                GetUsers();
            }
        }
    }
}
function SendMsg(MsgContent) {
    SendData = "{\"MsgType\":\"s \",\"SessionId\":\"" + SessionId + "\",\"MsgColor\":\"" + MsgColor + "\",\"MsgContent\":\"" + MsgContent + "\"}";
    doSend("s",SendData);
}
function SendMsgCallBack() {
    if (sins.readyState == 4){
        if (sins.status && sins.status == 200) {
            if (sins.responseText == "0") {
                alert("Server Refused");
                window.location.reload();
            } else if (iins.responseText == "2") {
                alert("Server Error ! \nPlease Send The Details To fdcube@gmail.com\nWe Will Be Very Happy To Recieve Your Letter! ");
                window.location.reload();
            } else {
                SendMsgHa(sins.responseText);
            }
        }
    }
}
function GetMsg() {
    SendData = "{\"MsgType\":\"g\",\"SessionId\":\"" + SessionId + "\",\"MsgId\":\"" + MsgId + "\"}";
    doSend("g",SendData);
}
function GetMsgCallBack() {
    if (gins.readyState == 4){
        if (gins.status && gins.status == 200) {
            if (gins.responseText == "0") {
                alert("Server Refused");
                window.location.reload();
            } else if (gins.responseText == "2") {
                alert("Server Error ! \nPlease Send The Details To fdcube@gmail.com\nWe Will Be Very Happy To Recieve Your Letter! ");
                window.location.reload();
            } else {
                GetMsgHa(decodeURIComponent(gins.responseText.replace(/\+/g," ")));
            }
            GetMsg();
        }
    }
}
function GetUsers() {
    SendData = "{\"MsgType\":\"u\",\"OlUid\":\"" + OlUid + "\",\"SessionId\":\"" + SessionId + "\"}";
    doSend("u",SendData);
}
function GetUsersCallBack() {
    if (uins.readyState == 4){
        if (uins.status && uins.status == 200) {
            if (uins.responseText == "0") {
                alert("Server Refused");
                window.location.reload();
            } else if (uins.responseText == "2") {
                alert("Server Error ! \nPlease Send The Details To fdcube@gmail.com\nWe Will Be Very Happy To Recieve Your Letter! ");
                window.location.reload();
            } else {
                GetUsersHa(decodeURIComponent(uins.responseText.replace(/\+/g," ")));
            }
            GetUsers();
        }
    }
}
function LogOut() {
    SendData = "{\"MsgType\":\"o\",\"SessionId\":\"" + SessionId + "\"}";
    doSend("o",SendData);
}
function LogOutCallBack() {
    if (oins.readyState == 4){
        if (oins.status && oins.status == 200) {
            if (oins.responseText == "0") {
                alert("Server Refused");
                window.location.reload();
            } else if (oins.responseText == "2") {
                alert("Server Error ! \nPlease Send The Details To fdcube@gmail.com\nWe Will Be Very Happy To Recieve Your Letter! ");
                window.location.reload();
            } else {
                LogOutHa(oins.responseText);
            }
        }
    }
}
function doSend(MsgType,SendData) {
    SendData = encodeURIComponent(SendData);
    var ajaxTmp = new ajax();
    switch(MsgType) {
        case "i":
            iins = ajaxTmp.DoPost(SendData, LoginCallBack);
            break;
        case "s":
            sins = ajaxTmp.DoPost(SendData, SendMsgCallBack);
            break;
        case "g":
            gins = ajaxTmp.DoPost(SendData, GetMsgCallBack);
            break;
        case "u":
            uins = ajaxTmp.DoPost(SendData, GetUsersCallBack);
            break;
        case "o":
            oins = ajaxTmp.DoPost(SendData, LogOutCallBack);
            break;
        default:
            alert("MsgType Error!");
    }
}

//Define response msg handler
function LoginHa(ReData) {
    SessionId = ReData;
    LoginUi();
}
function SendMsgHa(ReData) {
    if(ReData == "1") {
        SendMsgUi();
    } else {
        alert("Message Send Error!");
    }
}
function GetMsgHa(ReData) {
    if(ReData != "1") {
        var tmp = eval('(' + ReData + ')');
        MsgId = tmp.msgId;
        GetMsgUi(tmp.userName,tmp.msgContent,tmp.msgColor);
    }
}
function GetUsersHa(ReData) {
    if(ReData != "1") {
        var tmp = eval('(' + ReData + ')');
        OlUid = tmp.OlUid;
        GetUsersUi(tmp.UserName);
    }
}
function LogOutHa(ReData) {
    if (ReData == "1") {
        LogOutUi();
    } else {
        alert("LogOut Failed");
    }
}